package com.example.springboottest.example.nio;

import java.nio.ByteBuffer;

public class NIOBufferTest {
    public static void main(String[] args) {
        //分配1024字节就是  1kb
        ByteBuffer buffer = ByteBuffer.allocate(1024);

        String words = "Hello World!";
        for (char c : words.toCharArray()) {
//            System.out.println(c);
            buffer.put((byte) c);
        }

        //翻转 等价于 buffer.limit(buffer.position()).position(0);
        buffer.flip();
        //读数据
        while (buffer.hasRemaining()) {
            char c = (char) buffer.get();
            System.out.print(c);
        }
    }
}
